package cn.wolfcode.luowowo.website.controller;

import cn.wolfcode.luowowo.website.annotion.RequireLogin;
import cn.wolfcode.luowowo.cache.util.RedisKeys;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.member.service.IUserInfoService;
import cn.wolfcode.luowowo.website.util.CookieUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
public class LoginController {

    @Reference
    private IUserInfoService userInfoService;

    @RequireLogin
    @RequestMapping("/hello")
    @ResponseBody
    public Object  hello(Long id){
        return userInfoService.get(id);
    }

    /**
     * 手机唯一性检查
     * @param phone
     * @return
     */
    @RequestMapping("/checkPhone")
    @ResponseBody
    public Object getUnique(String phone){
        boolean flag = userInfoService.checkUnique(phone);
        return !flag;
    }

    /**
     * 短信验证码
     */
    @RequestMapping("/sendVerifyCode")
    @ResponseBody
    public Object sendVerifyCode(String phone){

        userInfoService.sendVerifyCode(phone);

        return AjaxResult.SUCCESS;
    }

    /**
     * 注册
     * phone: 17631590649
     * nickname:
     * password:
     * rpassword:
     * verifyCode:
     */
    @RequestMapping("/userRegist")
    @ResponseBody
    public Object userRegist(String phone,String nickname,String password,String rpassword,String verifyCode){
        userInfoService.userRegist(phone,nickname,password,rpassword,verifyCode);
        return AjaxResult.SUCCESS;
    }

    /**
     * 用户登录
     */
    @RequestMapping("/userLogin")
    @ResponseBody
    public Object userLogin(HttpServletRequest request, HttpServletResponse response,String username, String password){
        String token = userInfoService.userLogin(username,password);

        //5. 创建cookie对象，以token作为name,tokean的值作为value
        // Cookie c = new Cookie("token","随机值"),
        //设置cookie时间为30分钟，并通过response对象将cookie上传到浏览器中
        CookieUtil.addCookie("token",token,RedisKeys.USER_LOGIN_TOKEN.getTime().intValue(),response);
        return AjaxResult.SUCCESS;
    }
}
